/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mechanatrix.terrain;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.shadow.DirectionalLightShadowRenderer;
import com.jme3.util.SkyFactory;

/**
 *
 * @author dnigels
 */
public class TestGasStorage extends SimpleApplication {

	private BulletAppState bulletAppState;

	public static void main(String[] args) {
		TestGasStorage app = new TestGasStorage();
		app.start();
	}

	private PhysicsSpace getPhysicsSpace() {
		return bulletAppState.getPhysicsSpace();
	}

	@Override
	public void simpleInitApp() {

		bulletAppState = new BulletAppState();
		stateManager.attach(bulletAppState);
		//bulletAppState.setDebugEnabled(true);

		this.flyCam.setMoveSpeed(100f);
		
		Box floorBox = new Box(512, 0.0f, 512);
		floorBox.scaleTextureCoordinates(new Vector2f(64, 64));
		Geometry floorGeometry = new Geometry("Floor", floorBox);
		floorGeometry.setShadowMode(RenderQueue.ShadowMode.Receive);
		floorGeometry.setMaterial(assetManager.loadMaterial("MatDefs/grass_02.j3m"));
		floorGeometry.addControl(new RigidBodyControl(0));
		rootNode.attachChild(floorGeometry);
		getPhysicsSpace().add(floorGeometry);
		
		Spatial storage = assetManager.loadModel("Models/architecture/gas storage.obj");
		storage.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
		storage.scale(FastMath.pow(2f, -7f)); //0.015625f
		rootNode.attachChild(storage);
		
        storage.addControl(new RigidBodyControl(0));
        bulletAppState.getPhysicsSpace().addAll(storage);
		
		initLighting();

		//this.cam.lookAtDirection(new Vector3f(0.5f, 0f, 0.5f), Vector3f.UNIT_Y);
	}

	private void initLighting() {

		AmbientLight light = new AmbientLight();
		light.setColor(ColorRGBA.LightGray);
		rootNode.addLight(light);

		DirectionalLight sun = new DirectionalLight();
		sun.setDirection(new Vector3f(-FastMath.QUARTER_PI, -FastMath.QUARTER_PI, 1.0f));
		rootNode.addLight(sun);

		DirectionalLightShadowRenderer dlsr = new DirectionalLightShadowRenderer(assetManager, 1024, 3);
		dlsr.setLight(sun);
		viewPort.addProcessor(dlsr);
		rootNode.attachChild(SkyFactory.createSky(assetManager, "Textures/Hot Summer Day.dds", false));
	}
}
